package com.sxkf.core.third.verify;

import com.aliyun.facebody20191230.Client;
import com.aliyun.facebody20191230.models.*;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.exception.SxkfException;
import org.springframework.stereotype.Component;

import java.net.URL;

@Slf4j
@Component
public class FaceUtil {

    private static final String ACCESS_KEY_ID = "LTAI5tGTJE1hmpB986yZdqG2";
    private static final String ACCESS_KEY_SECRET = "zG1sjPvSOg716vPcDTL2iweLy3grIG";


    public static void main(String[] args) {
        System.out.println(recognizeHandGesture("https://kekejiaoyou.oss-cn-hangzhou.aliyuncs.com/images//2024-11-06/672aedb80a48dc4cd09c735f.jpg"));
    }

    public static CompareFaceResponseBody.CompareFaceResponseBodyData compareFace(String img1, String img2) {
        try {
            com.aliyun.facebody20191230.Client client = new Client(getConfig());
            com.aliyun.facebody20191230.models.CompareFaceAdvanceRequest compareFaceRequest = new com.aliyun.facebody20191230.models.CompareFaceAdvanceRequest()
                    .setImageURLAObject(new URL(img1).openConnection().getInputStream())
                    .setImageURLBObject(new URL(img2).openConnection().getInputStream());
            RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            // 复制代码运行请自行打印 API 的返回值
            CompareFaceResponse compareFaceResponse = client.compareFaceAdvance(compareFaceRequest, runtime);
            System.out.println(compareFaceResponse.getStatusCode());
            System.out.println(compareFaceResponse.body);
            // 获取整体结果
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(compareFaceResponse)));
            return compareFaceResponse.body.data;
        } catch (TeaException e) {
            log.info("code:{},msg:{}",e.statusCode,e.getMessage());
            throw new SxkfException(e.statusCode,e.getMessage());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * type:
     * bg：无法识别的手势
     * ok：确认
     * palm：手掌
     * left：握拳且大拇指向左
     * right：握拳且大拇指向右
     * good：点赞（即握拳且大拇指向上）
     * mute：噤声（将食指放在嘴上即被识别为噤声）
     * down：握拳且大拇指向下
     *
     * Score:识别置信度，范围0~1。
     */
    public static RecognizeHandGestureResponseBody.RecognizeHandGestureResponseBodyData recognizeHandGesture(String img) {
        RecognizeHandGestureResponse recognizeHandGestureResponse;
        try {
            Client client = new Client(getConfig());
            RuntimeOptions runtime = new RuntimeOptions();
            RecognizeHandGestureAdvanceRequest request = new RecognizeHandGestureAdvanceRequest()
                    .setImageURLObject(new URL(img).openConnection().getInputStream())
                    .setAppId("gesture_app")
                    .setGestureType("gesture_recognition");
            recognizeHandGestureResponse = client.recognizeHandGestureAdvance(request, runtime);
        } catch (TeaException e) {
            log.info("code:{},msg:{}",e.statusCode,e.getMessage());
            throw new SxkfException(e.statusCode,e.getMessage());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println(recognizeHandGestureResponse.body);
        System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(recognizeHandGestureResponse)));
        return recognizeHandGestureResponse.body.data;
    }

    private static Config getConfig() {
        return new Config()
                .setAccessKeyId(ACCESS_KEY_ID)
                .setAccessKeySecret(ACCESS_KEY_SECRET)
                .setEndpoint("facebody.cn-shanghai.aliyuncs.com");
    }
}